package qimao.com.edu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.databind.ObjectMapper;

import qimao.com.edu.service.AuthorService;
import qimao.com.edu.service.impl.AuthorServiceImpl;

import java.util.HashMap;
import java.util.Map;


@WebServlet("/admin/deleteAuthor.do")
public class DeleteAuthorServlet extends HttpServlet {
	
    AuthorService as = new AuthorServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        
        try {
            // 获取请求参数
            Integer authorId = Integer.parseInt(request.getParameter("id"));
            if (authorId == null) {
                result.put("message", "缺少作家ID");
                response.getWriter().write(objectMapper.writeValueAsString(result));
                return;
            }
            // 执行删除操作（这里需要替换为真实业务逻辑）
            int row = as.deleteAuthorById(authorId);
            
            if (row == 1) {
                result.put("success", true);
                result.put("message", "作家删除成功");
            } else {
                result.put("message", "作家不存在或已被删除");
            }
        } catch (Exception e) {
            result.put("message", "删除失败: " + e.getMessage());
            e.printStackTrace();
        }
        
        response.getWriter().write(objectMapper.writeValueAsString(result));
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}